import json
import requests
from PyQt5.QtCore import QThread, pyqtSignal

"""获取版本列表
威廉我

二次修复:
630
"""


class MinecraftVersionFetcher(QThread):
    versionsDict = pyqtSignal(dict)

    def __init__(self) -> None:
        super().__init__()
        self.url = "http://launchermeta.mojang.com/mc/game/version_manifest.json"
        self.releaseVersions = []
        self.snapshotVersions = []
        self.oldVerison = []

    def run(self):
        try:
            r = requests.get(self.url)
            print(r.text)
            result = None
        except Exception as e:
            raise Exception("版本列表请求失败")
        else:
            result = json.loads(r.text)

        if result:
            versions = result["versions"]
            for version in versions:
                if version["type"] == "release":
                    self.releaseVersions.append(version)
                elif version["type"] == "snapshot":
                    self.snapshotVersions.append(version)
                else:
                    self.oldVerison.append(version)

        versionsDict = {
            "release": self.releaseVersions,
            "snapshot": self.snapshotVersions,
            "old": self.oldVerison,
        }
        self.versionsDict.emit(versionsDict)

        self.quit()